package com.budwk.app.sqfwV2.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.models.Sqfw_formSummary;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfw.vo.Sqfw_formSummaryForAdminVO;
import com.budwk.app.sqfwDoc.dto.TemplateCodeUpdateDTO;
import com.budwk.app.sqfwV2.dto.MeetingParamDTO;
import com.budwk.app.sqfwV2.dto.TemplateFileDTO;
import com.budwk.app.sqfwV2.enums.MeetingEnums;
import com.budwk.app.sqfwV2.generator.meeting.MeetingPdfGenerator;
import com.budwk.app.sqfwV2.service.meeting.MeetingExcelService;
import com.budwk.app.sqfwV2.service.meeting.MeetingPdfService;
import com.budwk.app.sqfwV2.service.meeting.MeetingService;
import com.budwk.starter.common.exception.BaseException;
import com.budwk.starter.common.openapi.annotation.*;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;

import java.util.ArrayList;
import java.util.List;

@IocBean
@At("/platform/sqfwV2/meeting")
@SLog(tag = "会议/活动/来访记录")
@ApiDefinition(tag = "会议/活动/来访记录")
@Slf4j
public class MeetingController {
    @Inject
    private MeetingPdfService meetingPdfService;
    @Inject
    private SqfwFormsummaryNewService sqfwFormsummaryNewService;

    @Inject
    private MeetingExcelService meetingExcelService;

    @Inject
    private MeetingService meetingService;


    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "APP:提交数据")
    @ApiFormParams(implementation = TemplateCodeUpdateDTO.class)
    @SaCheckLogin
    public Result<?> addMeetingMinutes(@Param("..") TemplateCodeUpdateDTO dto) {
        log.info("MeetingController.addMeetingMinutes param: {}", JSON.toJSONString(dto));
        MeetingParamDTO meetingParamDTO = JSONObject.parseObject(dto.getFormSummary().getParamData(), MeetingParamDTO.class);
        if (ObjectUtil.isEmpty(meetingParamDTO.getType())) {
            throw new BaseException("meetingType不能为空");
        }
        //dto.getCompleteFlow().setFileType(FormSummaryTypeEnums.MEETING.getCode());
        meetingPdfService.addGeneratorQueue(new MeetingPdfGenerator(MeetingEnums.getTemplatName(meetingParamDTO.getType())));
        meetingPdfService.generatePdfAndMessage(SecurityUtil.getUserId(), dto);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    public Result<?> changeTemplate(@Param(value = "sqfwId") String sqfwId) {
        List<TemplateFileDTO> meetingFileDTOList = new ArrayList<>();
        int hyjlCount = sqfwFormsummaryNewService.count(Cnd.where(Sqfw_formSummary_new::getSqfwId, "=", sqfwId)
                .and(" param_data ->>'meetingType'", "=", MeetingEnums.MEETING_MINUTES_DOCX.getName()));
        int activitiesCount = sqfwFormsummaryNewService.count(Cnd.where(Sqfw_formSummary_new::getSqfwId, "=", sqfwId)
                .and(" param_data ->>'meetingType'", "=", MeetingEnums.ACTIVITIES_DOCX.getName()));
        int visitingCount = sqfwFormsummaryNewService.count(Cnd.where(Sqfw_formSummary_new::getSqfwId, "=", sqfwId)
                .and(" param_data ->>'meetingType'", "=", MeetingEnums.VISITING_RECORDS_DOCX.getName()));
        String hyjlLabe = "暂无数据";
        String activitiesLabe = "暂无数据";
        String visitingLabe = "暂无数据";
        if (hyjlCount > 0) {
            hyjlLabe = "全部数据";
        }
        if (activitiesCount > 0) {
            activitiesLabe = "全部数据";
        }
        if (visitingCount > 0) {
            visitingLabe = "全部数据";
        }
        for (MeetingEnums enumObj : MeetingEnums.values()) {
            if (enumObj.isLoopLable()) {
                TemplateFileDTO meetingFileDTO = new TemplateFileDTO();
                meetingFileDTO.setCode(enumObj.getCode());
                meetingFileDTO.setFileNname(enumObj.getName());
                if (enumObj.getCode() == MeetingEnums.MEETING_MINUTES_EXCEL.getCode() | MeetingEnums.MEETING_MINUTES_DETAIL_EXCEL.getCode() == enumObj.getCode()) {
                    meetingFileDTO.setDataLabel(hyjlLabe);
                } else if (enumObj.getCode() == MeetingEnums.ACTIVITIES_EXCEL.getCode()) {
                    meetingFileDTO.setDataLabel(activitiesLabe);
                } else if (enumObj.getCode() == MeetingEnums.VISITING_RECORDS_EXCEL.getCode()) {
                    meetingFileDTO.setDataLabel(visitingLabe);
                } else {
                    if (hyjlCount > 0 | activitiesCount > 0 | visitingCount > 0) {
                        meetingFileDTO.setDataLabel("全部数据");
                    } else {
                        meetingFileDTO.setDataLabel("暂无数据");
                    }
                }
                meetingFileDTOList.add(meetingFileDTO);
            }
        }
        return Result.success(meetingFileDTOList);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "APP:根据模板标号下载对应文档")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateCode", description = "文件标号", required = true, check = true),
            @ApiImplicitParam(name = "isTemplate", description = "下载模板标识", required = true, check = true),
            @ApiImplicitParam(name = "sqwfIds", description = "服务标识", required = true, check = true)
    })
    @SaCheckLogin
    public Result<?> generatorFile(@Param(value = "templateCode") Integer templateCode, @Param(value = "isTemplate") boolean isTemplate, @Param("sqwfIds") String sqwfIds) {
        log.info("MeetingController.generatorFile templateCode: {},isTemplate:{},sqwfIds:{}", templateCode, isTemplate, sqwfIds);
        MeetingEnums meetingEnums = MeetingEnums.getTemplatEnums(templateCode);
        if (ObjectUtil.isEmpty(meetingEnums)) {
            throw new BaseException("templateCode有误");
        }
        return Result.data(meetingService.generatorFile(templateCode, isTemplate, sqwfIds));
    }


    @At
    @Ok("json")
    @ApiOperation(name = "APP:数据检测")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sqfwId", description = "服务ID", required = true, check = true)
    })
    @POST
    public Result<?> checkData(@Param(value = "sqfwId") String sqfwId) {
        log.info("MeetingController.checkData sqfwId: {}", sqfwId);
        return Result.data(meetingService.checkData(sqfwId, false));
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "重新生成PDF")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "completeflowId", description = "工作流id", required = true, check = true)
    })
    @SaCheckLogin
    public Result<?> afreshGeneratePDF(@Param("completeflowId") String completeflowId) {
        log.info("MeetingController.afreshGeneratePDF completeflowId: {}", completeflowId);
        return meetingPdfService.afreshGeneratePDF(completeflowId);
    }

    @At()
    @Ok("json")
    @POST
    @ApiOperation(name = "后台管理系统任务列表查询")
    @ApiFormParams(implementation = Sqfw_formSummaryForAdminVO.class)
    @ApiResponses(implementation = Sqfw_formSummary.class)
    @SaCheckLogin
    public Result<?> listSummaryMeetingByPage(@Param("..") Sqfw_formSummaryForAdminVO forAdminVo) {
        log.info("MeetingMinutesController listSummaryByPage param: {}", JSONObject.toJSONString(forAdminVo));
        return Result.data(meetingService.listSummaryMeetingByPage(forAdminVo));
    }

    @At()
    @Ok("json")
    @POST
    @ApiOperation(name = "后台:导出")
    @ApiFormParams(implementation = Sqfw_formSummaryForAdminVO.class)
    @SaCheckLogin
    public Result<?> generateMeetingExcelCollZipForBM(@Param("..") Sqfw_formSummaryForAdminVO forAdminVo, @Param(value = "templateCode") Integer templateCode, @Param(value = "isTemplate") boolean isTemplate, @Param(value = "entry") String entry, @Param(value = "ids") String ids) {
        MeetingEnums meetingEnums = MeetingEnums.getTemplatEnums(templateCode);
        if (ObjectUtil.isEmpty(meetingEnums)) {
            throw new BaseException("templateCode有误");
        }
        return Result.data(meetingService.generateMeetingExcelCollZipForBM(forAdminVo, templateCode, isTemplate, entry, ids));
    }

    @At()
    @Ok("json")
    @POST
    @ApiOperation(name = "后台：数据检测")
    @ApiFormParams(implementation = Sqfw_formSummaryForAdminVO.class)
    @SaCheckLogin
    public Result<?> generateMeetingCheckDataForAdmin(@Param("..") Sqfw_formSummaryForAdminVO forAdminVo, @Param(value = "entry") String entry, @Param(value = "ids") String ids) {
        return Result.data(meetingService.generateMeetingCheckDataForAdmin(forAdminVo, entry, ids));
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "查询会议名称历史数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", description = "记录类型", required = true, check = true),
            @ApiImplicitParam(name = "theme", description = "主题", required = true, check = true)
    })
    @SaCheckLogin
    public Result<?> queryMeetNameHistory(@Param("type") String type, @Param("theme") String theme) {
        log.info("MeetingController queryMeetNameHistory type：{},theme: {}", type, theme);
        return meetingService.queryMeetNameHistory(type,theme);
    }
}
